# Author    : CoffeeChicken
# Date      : 2020-10-09 11:17
# Function  : 演示 爬取站长之间免费简历模板

import requests
from lxml import etree

url_front_page = "http://sc.chinaz.com/jianli/free.html"

headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/85.0.4183.121 Safari/537.36"
}
# 获取前端网页
response_front_page = requests.get(url_front_page, headers).text
# 加载到etree对象中
tree = etree.HTML(response_front_page)
# 解析出所有简历地址
link_url_list = tree.xpath("//div[@id='main']/div/div/p/a/@href")
# 遍历所有简历地址
for index in range(len(link_url_list)):

    response_download = requests.get(link_url_list[index], headers).text

    tree_download = etree.HTML(response_download)
    # 解析出简历下载地址
    download_url = tree_download.xpath("//div[@id='down']/div[2]/ul/li[1]/a/@href")
    # 获取压缩包二进制文件
    download_rar = requests.get(download_url[0], headers).content
    # 保存在本地
    with open("resumes/resume" + str(index+1) + ".rar", "wb") as fp:
        fp.write(download_rar)
